import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

var router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('../views/index/Home.vue'),
            meta: {
                title: '主页'
            }
        },{
          path: '/shopinfo',
          name: 'shopinfo',
          component: () => import('../views/shopinfo/Home.vue'),
          meta: {
            title: '商品信息'
          }
        },{
          path: '/shopcart',
          name: 'shopcart',
          component: () => import('../views/shopcart/Home.vue'),
          meta: {
            title: '购物车'
          }
        },{
          path: '/pay',
          name: 'pay',
          component: () => import('../views/pay/Home.vue'),
          meta: {
            title: '支付'
          }
        },{
          path: '/login',
          name: 'login',
          component: () => import('../views/login/Home.vue'),
          meta: {
            title: '登录'
          }
        },{
          path: '/register',
          name: 'register',
          component: () => import('../views/register/Home.vue'),
          meta: {
            title: '注册'
          }
        }
    ],
     //跳转页面后置顶
  scrollBehavior(to,from,savedPosition){
    if(savedPosition){
      return savedPosition;
    }else{
      return {x:0,y:0}
    }
  }
})

export default router
//全局守卫
router.beforeEach((to, from, next) => {
    /* 路由发生变化时修改页面title */
    if (to.meta.title) {
      document.title = to.meta.title
    }
    next()
  })




